package com.my.commonlibrary.fragment;

import android.net.Network;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.my.commonlibrary.R;
import com.my.commonlibrary.base.BaseFragment;
import com.my.commonlibrary.net.retrofit.ApiManager;
import com.my.commonlibrary.net.retrofit.UserModel;
import com.my.commonlibrary.utils.ToastUtil;
import com.tamic.novate.Novate;

import java.io.IOException;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Author : Cooper
 * Time : 2016/10/31  15:50
 * Description : Retrofit 应用
 */

public class H_RetrofitFragment extends BaseFragment {

    @Bind(R.id.load_bt)
    Button loadBt;
    @Bind(R.id.respone_content_tv)
    TextView responeContentTv;

    @Override
    protected int getFragmentLayoutRes() {
        return R.layout.frag_home_retrofit_layout;
    }

    @Override
    protected void initFragmentData() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.load_bt)
    public void onClick() {
        novateRequest();
    }

    /**
     * 封装Retrofit2.0的请求数据
     */
    private void novateRequest() {
        Novate novate = new Novate.Builder(getActivity())
                .baseUrl(ApiManager.BASE_RUL)
                .build();
        HashMap<String, String> pamarms = new HashMap<>();
        pamarms.put("auth_code","CB53A41C2C403A19C14DA32609FA2C7F");
        pamarms.put("user_id","576796");
        novate.executeGet("mars/user/v1/detail", pamarms, new Novate.ResponseCallBack<UserModel>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.showToast("错误了~~~");
            }

            @Override
            public void onSuccee(UserModel response) {
                responeContentTv.setText(response.getData().toString());
            }
        });
    }

    /**
     * 原生Retrofit2.0请求数据
     */
    private void retrofitRequest() {
        Retrofit retrofit = new Retrofit.Builder()
                .client(new OkHttpClient.Builder().addInterceptor(new Interceptor() { // 添加headder
                    @Override
                    public okhttp3.Response intercept(Chain chain) throws IOException {
                        Request request = chain.request().newBuilder()
                                .addHeader("mac", "11111")
                                .addHeader("uuid", "gdeflatfgfg5454545e")
                                .addHeader("userId", "123456")
                                .addHeader("netWork", "wifi")
                                .addHeader("test", "for_test")
                                .build();
                        return chain.proceed(request);
                    }
                }).build())
                .baseUrl(ApiManager.BASE_RUL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiManager apiManager = retrofit.create(ApiManager.class);
        Call<UserModel> call = apiManager.getData("CB53A41C2C403A19C14DA32609FA2C7F", "576796");
        call.enqueue(new Callback<UserModel>() {
            @Override
            public void onResponse(Call<UserModel> call, Response<UserModel> response) {
                responeContentTv.setText(response.body().getData().toString());
            }

            @Override
            public void onFailure(Call<UserModel> call, Throwable t) {
                ToastUtil.showToast("错误了~~~");
            }
        });
    }
}
